package org.yjw;

import java.io.*;

public class FileCopyFileTest {

    public static void main(String[] args) {
        FileCopyFileTest test = new FileCopyFileTest();

        //电脑d盘中的baoming.png 图片的路径
        String filePath1 = "D:/baoming.png";

        //电脑d盘中的baoming2.png 图片的路径
        String filePath2 = "D:/baoming2.png";

        //复制文件
    }

    /**
     * 文件复制
     * FileOutputStream
     * FileInputStream
     *
     * @param filePath_old : 需要复制文件的路径
     * @param filePath_new : 复制文件存放的路径
     */
    public void copyFile(String filePath_old, String filePath_new) {
        FileInputStream fis = null;
        FileOutputStream fout = null;
        try {
            //1.根据path路径实例化一个输入流的对象
            fis = new FileInputStream(filePath_old);
            //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值；
            int size = fis.available();
            //3. 根据输入流中的字节数创建byte数组；
            byte[] array = new byte[size];
            //4.把数据读取到数组中；
            fis.read(array);
            //5、根据文件路径创建输出流
            fout = new FileOutputStream(filePath_new);
            //5、把byte数组输出；
            fout.write(array);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fout != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 复制文件
     * BufferOutputStream
     * BufferInputStream
     *
     * @param oldFile
     * @param newFile
     */
    public void copyFile(File oldFile, File newFile) {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;

        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            inputStream = new FileInputStream(oldFile);
            bufferedInputStream = new BufferedInputStream(inputStream);
            outputStream = new FileOutputStream(newFile);
            bufferedOutputStream = new BufferedOutputStream(outputStream);

            byte[] b = new byte[1024];   //代表一次最多读取1KB的内容

            int length = 0; //代表实际读取的字节数
            while ((length = bufferedInputStream.read(b)) != -1) {
                //length 代表实际读取的字节数
                bufferedOutputStream.write(b, 0, length);
            }
            //缓冲区的内容写入到文件
            bufferedOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 复制文件
     * BufferedReader
     * BufferedWriter
     *
     * @param oldFile
     * @param newFile
     */
    private void copyFile1(File oldFile, File newFile) {
        Reader reader = null;
        BufferedReader bufferedReader = null;

        Writer writer = null;
        BufferedWriter bufferedWriter = null;
        try {
            reader = new FileReader(oldFile);
            bufferedReader = new BufferedReader(reader);

            writer = new FileWriter(newFile);
            bufferedWriter = new BufferedWriter(writer);

            String result = null; //每次读取一行的内容
            while ((result = bufferedReader.readLine()) != null) {
                bufferedWriter.write(result);  //把内容写入文件
                bufferedWriter.newLine();  //换行，result 是一行数据，所以没写一行就要换行
            }
            bufferedWriter.flush();  //强制把数组内容写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();  //关闭输出流
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                bufferedReader.close();  //关闭输入流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 复制文件
     * OutputStreamWriter
     * InputStreamReader
     *
     * @param oldFile
     * @param newFile
     */
    public void copyFile2(File oldFile, File newFile) {
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;

        try {
            inputStream = new FileInputStream(oldFile); //创建输入流
            inputStreamReader = new InputStreamReader(inputStream); //创建转换输入流
            outputStream = new FileOutputStream(newFile); //创建输出流
            outputStreamWriter = new OutputStreamWriter(outputStream); //创建转换输出流
            int result = 0;

            while ((result = inputStreamReader.read()) != -1) {  //一次只读一个字符
                outputStreamWriter.write(result); //一次只写一个字符
            }
            outputStreamWriter.flush();  //强制把缓冲写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (outputStreamWriter != null) {
                try {
                    outputStreamWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

